package com.shyb.lingxian.mine.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import com.shyb.lingxian.Util.AppUtil;
import com.shyb.lingxian.Util.CheckSumBuilder;
import com.shyb.lingxian.Util.SPUtils;
import com.shyb.lingxian.Util.ToastUtil;
import com.shyb.lingxian.base.BasePresenter;
import com.shyb.lingxian.mine.bean.LoginBean;
import com.shyb.lingxian.mine.bean.TicketBean;
import com.shyb.lingxian.mine.view.LoginView;
import com.shyb.lingxian.netWork.Const;
import com.shyb.lingxian.netWork.RequestCallback;

import rx.Observable;

/**
 * Created by gs on 2018/3/23.
 */

public class LoginPresenter extends BasePresenter<LoginView> {

    private String ticket;

    public LoginPresenter(LoginView view) {
        super.attachView(view);
    }

    //获取ticket
    public void getTicket(final Context context){
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String checkSum = CheckSumBuilder.getCheckSum("1", Const.NONCE, timestamp);
        onSubscribe(services.getTicket(Const.secret, Const.NONCE, timestamp, checkSum), new RequestCallback<TicketBean>() {
            @Override
            public void onSuccess(TicketBean model) {
                if (model != null){
                    String code = model.getCode();
                    if (code != null && code.equals("00")){
                        ticket = model.getTicket();
                        SPUtils.saveString(context,"TICKETS",ticket);
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                ToastUtil.showToast(context,message);
            }

            @Override
            public void onFinish() {

            }
        });
    }

    //获取验证码
    public void getCOde(final Context context){
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String checkSum = CheckSumBuilder.getCheckSum("1", Const.NONCE, timestamp);
        String ticket1 = this.ticket;
        if (ticket1== null || ticket1.length()==0){
            Log.e("Ticket参数异常",ticket1);
            return;
        }

        String phone = view.getPhone();
        if (!AppUtil.isPhoneNumber(phone)){
            return;
        }

        onSubscribe(services.SendCode(Const.secret,Const.NONCE,timestamp,checkSum,ticket1,phone), new RequestCallback<TicketBean>() {
            @Override
            public void onSuccess(TicketBean model) {
                if (model != null){
                    String code = model.getCode();
                    if (code != null && code.equals("00")){
                        view.getCodeSucess();
                    }else {
                        ToastUtil.showToast(context,"sorry 验证码发送失败！");
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                ToastUtil.showToast(context,message);
            }

            @Override
            public void onFinish() {

            }
        });
    }


    //登录
    public void Login(final Context context){
        String tiemStmp = String.valueOf(System.currentTimeMillis()/1000);
        String checkSum = CheckSumBuilder.getCheckSum("1", Const.NONCE, tiemStmp);
        String ticket1 = this.ticket;
        if (ticket1== null || ticket1.length()==0){
            Log.e("Ticket参数异常",ticket1);
            return;
        }

        String phone = view.getPhone();
        if (!AppUtil.isPhoneNumber(phone)){
            return;
        }

        String code = view.getCode();
        if (code == null || code.length() == 0){
            Log.e("loginActivity：","验证码异常");
            return;
        }

        onSubscribe(services.Login(Const.secret, Const.NONCE, tiemStmp, checkSum, ticket1, phone, code), new RequestCallback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean model) {
                if (model != null){
                    String code1 = model.getCode();
                    if (code1 != null && code1.equals("00")){
                        view.LoginSuccess();
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                ToastUtil.showToast(context,message);
            }

            @Override
            public void onFinish() {

            }
        });
    }


}
